// 
// OV-chip 2.0 project
// 
// Digital Security (DS) group at Radboud Universiteit Nijmegen
// 
// Copyright (C) 2008, 2009
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License in file COPYING in this or one of the
// parent directories for more details.
// 
// Created 26.8.08 by Hendrik
// 
// generate applet id strings
// 
// $Id: Applet_id.java,v 1.3 2009-02-20 15:29:30 tews Exp $


// throw away program, no package

public class Applet_id {

    public static void usage() {
        System.out.println("java Applet_id: generate applet id strings");
        System.out.println("Usage: java Applet_id [-java] [-package] id");
        System.out.println("  -java     generate a java byte array " +
                           "init expression");
        System.out.println("  -package  generate only the package part");
        System.out.println("  -help     print usage info");

    }

    private static String id = null;

    private static boolean java_expression = false;

    private static boolean only_package = false;

    public static void main(String args[]) {
        int i = 0;

        while(i < args.length) {
            if(args[i].equals("-help") ||
               args[i].equals("--help") ||
               args[i].equals("-h")) {
                usage();
                System.exit(0);
            }
            else if(args[i].equals("-java")) {
                java_expression = true;
            }
            else if(args[i].equals("-package")) {
                only_package = true;
            }
            else if(args[i].charAt(0) == '-') {
                usage();
                System.exit(1);
            }
            else if(id == null) {
                id = args[i];
            }
            else {
                usage();
                System.exit(1);
            }
            i++;
        }

        if(id == null) {
            usage();
            System.exit(1);
        }

        if(id.length() < 5 || id.length() > 16) {
            System.err.println("An application identifier is between 5 and " +
                               "16 characters long!");
            System.exit(1);
        }

        int col_count = 0;
        if(java_expression) {
            System.out.format("        // Application ID %s generated by " +
                              "Applet_id\n",
                              id);
            System.out.format("        ");
            col_count += 8;
        }
        for(int j = 0; j < id.length(); j++) {
            if(id.charAt(j) == '.' && only_package)
                break;

            if(java_expression) {
                if(j > 0) {
                    System.out.format(", ");
                    col_count += 2;
                    if(col_count > 65) {
                        System.out.format("\n        ");
                        col_count = 8;
                    }
                }
                System.out.format("(byte)0x%02X", id.codePointAt(j));
                col_count += 10;
            }
            else {
                if(j > 0)
                    System.out.format(":");
                System.out.format("0x%02x", id.codePointAt(j));
            }
        }
        System.out.println("");
    }
}
